package com.my.blog.website.controller.user;

import com.github.pagehelper.PageInfo;
import com.my.blog.website.constant.WebConst;
import com.my.blog.website.controller.BaseController;
import com.my.blog.website.dto.MetaDto;
import com.my.blog.website.dto.Types;
import com.my.blog.website.modal.Vo.ContentVo;
import com.my.blog.website.service.IContentService;
import com.my.blog.website.service.IMetaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;


/**
 * Created by geng on 2019/10/23.
 */
@Controller
public class TypeShowController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TypeShowController.class);
    @Autowired
    private IMetaService metaService;
    @Autowired
    private IContentService contentService;


    /**
     * 获取分类下的文章
     *
     * @param
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/types/{id}")
    public String types(@PathVariable Integer id, Pageable page,
                        Model model) {
        //   PageHelper.startPage(pageNum,pageSize);
        List<MetaDto> list = metaService.getMetaList(Types.CATEGORY.getType(), null, WebConst.MAX_POSTS);
        if (id < 0) {
            id = list.get(0).getMid();
        }
        PageInfo<ContentVo> contentsPaginator = contentService.getArticles(id, page.getPageNumber(), WebConst.DEFAULT_PAGE_SIZE);
        model.addAttribute("types", list);
        model.addAttribute("page", contentsPaginator);
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}
